package org.batatasframework.support.editable.internal;

import java.util.HashMap;
import java.util.Map;

import org.batatasframework.support.database.jdbc.ConnectionAccessor;
import org.batatasframework.support.definition.TableDefinition;
import org.batatasframework.web.FormRender;
import org.springframework.ui.Model;
import org.springframework.web.servlet.ModelAndView;


/**
 * 用于生成编辑或查看的界面
 * @author jackson
 *
 */
public abstract class AbstractFormRender implements FormRender{
	
	/**
	 * 这个字段可以告诉界面，当前的这个指标界面是否是可编辑的界面；
	 * 因为查看和编辑都是相同的一个界面
	 */
	private final transient boolean editable;
	
	/** 这里的处理就完全跳过了hibernate的框架了 */
	private final transient TableDefinition tableDefinition; 
	
	private final Map<String, FormItem> itemMapped = new HashMap<>();

	protected ConnectionAccessor connectionAccessor;
	
	private Object ownerEntity;//不一定有值
	
	/** 该表唯一的ID值 */
	private Object id;
	
	public AbstractFormRender(boolean editable, TableDefinition tableDefinition){
		this.editable = editable;
		this.tableDefinition = tableDefinition;
	}
	
	public abstract class FormItem{
		public String getHtml(boolean editable){
			return "";
		}
	}
	
	public void render(Model model){
		model.addAllAttributes(itemMapped);
	}
	
	public void render(ModelAndView mav){
		mav.addAllObjects(itemMapped);
	}
}
